package com.akino.gulimall.ware.listener;

import com.akino.common.to.mq.OrderTo;
import com.akino.common.to.mq.StockDetailTo;
import com.akino.common.to.mq.StockLockedTo;
import com.akino.common.utils.R;
import com.akino.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.akino.gulimall.ware.entity.WareOrderTaskEntity;
import com.akino.gulimall.ware.service.WareSkuService;
import com.akino.gulimall.ware.vo.OrderVo;
import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author Akino
 * @version 1.0
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    WareSkuService wareSkuService;
    /**
     * 处理库存锁定释放的请求
     */
    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo to, Message message, Channel channel) throws IOException {
        try {
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    /**
     * 订单关闭后解锁库存
     * @param to 关闭的订单
     * @param message
     * @param channel
     */
    @RabbitHandler
    public void handleOrderClose(OrderTo to,Message message, Channel channel) throws IOException {
        try {
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
